package com.dyh.quickdemozwei.dyhTp;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yhdong
 * @Date: 2021/10/1 14:38
 */
@Slf4j
public class TestDyhThreadPool {

    Runnable r = ()->{
        log.info("开始 {}",Thread.currentThread().getName());
        try {
            Thread.sleep(1500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("结束 {}",Thread.currentThread().getName());
    };

    /*2s*/
    @Test
    public void test1() throws InterruptedException {
        DyhQueue<Runnable> dyhQueue = new DyhQueue<>(2);
        DyhThreadPool dyhThreadPool = new DyhThreadPool(
                1,3,120, TimeUnit.SECONDS,
                dyhQueue,
                new ThreadFactoryBuilder().setNameFormat("TestDyhThreadPool-%d").build(),
             //   new ThreadPoolExecutor.CallerRunsPolicy()
                new ThreadPoolExecutor.AbortPolicy()
        );

        dyhQueue.setExecutor(dyhThreadPool);//bind

        /*
        14:56:26.097 [TestDyhThreadPool-0] INFO com.dyh.quickdemozwei.dyhTp.TestDyhThreadPool - 开始 TestDyhThreadPool-0
        14:56:26.097 [TestDyhThreadPool-2] INFO com.dyh.quickdemozwei.dyhTp.TestDyhThreadPool - 开始 TestDyhThreadPool-2
        14:56:26.097 [TestDyhThreadPool-1] INFO com.dyh.quickdemozwei.dyhTp.TestDyhThreadPool - 开始 TestDyhThreadPool-1
        14:56:26.602 [TestDyhThreadPool-1] INFO com.dyh.quickdemozwei.dyhTp.TestDyhThreadPool - 结束 TestDyhThreadPool-1
        14:56:26.602 [TestDyhThreadPool-0] INFO com.dyh.quickdemozwei.dyhTp.TestDyhThreadPool - 结束 TestDyhThreadPool-0
        14:56:26.603 [TestDyhThreadPool-2] INFO com.dyh.quickdemozwei.dyhTp.TestDyhThreadPool - 结束 TestDyhThreadPool-2
        */



        for (int i = 0;i<10;i++){
            dyhThreadPool.execute(r);
        }

        Thread.sleep(60000);
    }

    /*5s*/
    @Test
    public void test2() throws InterruptedException {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,3,120, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2),
                new ThreadFactoryBuilder().setNameFormat("TestDyhThreadPool-%d").build(),
                //   new ThreadPoolExecutor.CallerRunsPolicy()
                new ThreadPoolExecutor.AbortPolicy()
        );




        for (int i = 0;i<3;i++){
            threadPoolExecutor.execute(r);
        }

        Thread.sleep(60000);
    }

    /*2s*/
    @Test
    public void test3() throws InterruptedException {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,3,120, TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                new ThreadFactoryBuilder().setNameFormat("TestDyhThreadPool-%d").build(),
                //   new ThreadPoolExecutor.CallerRunsPolicy()
                new ThreadPoolExecutor.AbortPolicy()
        );




        for (int i = 0;i<10;i++){
            threadPoolExecutor.execute(r);
        }

        Thread.sleep(60000);
    }

    /*类似 SynchronousQueue 加速new线程*/
}
